import {Component} from '@angular/core';
import {IonicPage, NavController, NavParams} from 'ionic-angular';
import {ConfigProvider} from "../../providers/config/config";
import {ValidateProvider} from "../../providers/validate/validate";
import {HttpServicesProvider} from "../../providers/http-services/http-services";
import {StorageProvider} from "../../providers/storage/storage";
import { UtilsProvider } from '../../providers/utils/utils';

@IonicPage({
    segment: 'member/task/reward',
    defaultHistory: ['MemberPage']
})
@Component({
    selector: 'page-person-reward',
    templateUrl: 'person-reward.html',
})
export class PersonRewardPage {

    constructor(
        public navCtrl: NavController,
        public navParams: NavParams,
        public config: ConfigProvider,
        public validate: ValidateProvider,
        public httpService: HttpServicesProvider,
        public storage: StorageProvider,
        public util: UtilsProvider
    ) {
    }

    //判断用户是否登录
    ionViewCanEnter() {
        return this.validate.checkLogin();
    }

    ionViewWillEnter() {
        this.getData(null);
    }

    page = 1;
    lists = [];

    getData(infiniteScroll) {
        let apiUrl = "/User_info/my_push_award";
        this.httpService.requestPostData(apiUrl, {
            token: this.config.getToken(),
            page: this.page
        }, (response) => {
            //全部数据
            let data = response['data'];
            this.lists = this.lists.concat(data['lists']);
            this.page++;
            if (infiniteScroll) {
                infiniteScroll.complete();
                /*请求完成数据更新页面*/
                if (data['lists'].length < data['page_size']) {   /*没有数据 禁用上拉更新*/
                    infiniteScroll.enable(false);
                }
            }
        });
    }

    doInfinite(infiniteScroll) {
        this.getData(infiniteScroll);
    }

    pageRedirect(page,data={}){
        this.navCtrl.push(page,data).then(success=>{
            if(success !== true){//页面需要登录后跳转
                this.navCtrl.push('LoginPage',data);
            }
        })
    }
    changeStatus(amoy_id, status, user_id) {
        this.util.presentConfirm('确定要修改当前任务状态吗？', ()=>{
            let apiUrl = "/task_info/change_status";
            this.httpService.requestPostData(apiUrl, {
                token: this.config.getToken(),
                id: amoy_id,
                status: status,
                user_id: user_id
            }, data => {
                if(data['error'] == 200) {
                    this.util.presentAlert('操作成功');
                    // this.getData(null);
                    location.reload();
                } else {
                    this.util.presentAlert(data.message);
                }
            })
        });
    }
}
